﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;

namespace HobSoft.SoccerBet2014.Domain.Models
{
    using Core;
    [Serializable]
    [DataContract]
    public class BFEvent : IModel
    {
        public BFEvent()
        {
            Markets = new ObservableCollection<BFMarket>();
        }

        private int _eventID;
        [Key]
        [PrimaryKey]
        [Required]
        [DataMember(IsRequired = true)]
        public int EventID
        {
            get { return _eventID; }
            set { _eventID = value; }
        }

        private string _eventPath;
        [Required]
        [MaxLength(500)]
        [MinLength(1)]
        [DataMember(IsRequired = true)]
        public string EventPath
        {
            get { return _eventPath; }
            set { _eventPath = value; }
        }

        private string _homeTeam;
        [Required]
        [MaxLength(100)]
        [MinLength(1)]
        [DataMember(IsRequired = true)]
        public string HomeTeam
        {
            get { return _homeTeam; }
            set { _homeTeam = value; }
        }

        private string _awayTeam;
        [Required]
        [MaxLength(100)]
        [MinLength(1)]
        [DataMember(IsRequired = true)]
        public string AwayTeam
        {
            get { return _awayTeam; }
            set { _awayTeam = value; }
        }

        private DateTime _startTime;
        [Required]
        [DataMember(IsRequired = true)]
        public DateTime StartTime
        {
            get { return _startTime; }
            set { _startTime = value; }
        }

        private string _tournament;
        [MaxLength(50)]
        [MinLength(1)]
        [Required]
        [DataMember(IsRequired = true)]
        public string Tournament
        {
            get { return _tournament; }
            set { _tournament = value; }
        }

        private string _tournamentFullName;
        [MaxLength(200)]
        [DataMember()]
        public string TournamentFullName
        {
            get { return _tournamentFullName ?? string.Empty; }
            set { _tournamentFullName = value; }
        }

        private byte? _homeScore;
        [DataMember]
        public byte? HomeScore
        {
            get { return _homeScore ?? 0; }
            set { _homeScore = value; }
        }

        private byte? _awayScore;
        [DataMember]
        public byte? AwayScore
        {
            get { return _awayScore ?? 0; }
            set { _awayScore = value; }
        }

        private long _orderIndex;
        [DataMember(IsRequired = true)]
        [Required]
        public long OrderIndex
        {
            get { return _orderIndex; }
            set { _orderIndex = value; }
        }

        private string _displayTime;
        [DataMember]
        [MaxLength(50)]
        public string DisplayTime
        {
            get { return _displayTime ?? string.Empty; }
            set { _displayTime = value; }
        }

        private string _state;
        [DataMember]
        [MaxLength(20)]
        public string State
        {
            get { return _state ?? string.Empty; }
            set { _state = value; }
        }


        private bool? _notAvailable = false;
        [IgnoreDataMember]
        public bool? NotAvailable
        {
            get { return _notAvailable ?? true; }
            set { _notAvailable = value; }
        }

        private ObservableCollection<BFMarket> _markets;
        [IgnoreDataMember]
        public ObservableCollection<BFMarket> Markets
        {
            get { return _markets; }
            set
            {
                if (_markets == value) return;
                _markets = value;
                if (_markets != null)
                {
                    _markets.CollectionChanged += Markets_CollectionChanged;
                    foreach (var mk in _markets)
                    {
                        mk.Event = this;
                    }
                }
            }
        }

        private void Markets_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace)
            {
                foreach (BFMarket item in e.NewItems)
                {
                    if (item.Event != this)
                        item.Event = this;
                }
                if (e.OldItems != null)
                {
                    foreach (BFMarket item in e.OldItems)
                    {
                        item.Event = null;
                    }
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                if (e.OldItems != null)
                {
                    foreach (BFMarket item in e.OldItems)
                    {
                        item.Event = null;
                    }
                }
            }
        }
        

    }
}
